package com.yy.bolg.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yy.bolg.entity.FixedPointBean;
import com.yy.bolg.resp.hik.HikFireCloudDeviceResp;
import com.yy.bolg.service.FixedPointService;
import com.yy.bolg.util.hikfirecloud.HikFireCloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 定时每天凌晨1点从海康获取一次0点0分的传感器数据
 * @author enmaoFu
 * @date 2025-04-10
 */
@Service
public class DailyTaskServer {

    private HikFireCloud hikFireCloud = new HikFireCloud();

    /**
     * 自动载入Service对象
     */
    @Autowired
    private FixedPointService service;

    // @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
    // @Scheduled(cron = "0 */1 * * * ?") // 每1分钟执行一次
    // @Scheduled(cron = "30 59 23 * * ?") // 每天23:59:30执行
    @Scheduled(cron = "5 0 0 * * ?") // 每天00:00:05执行
    public void dailyTask() {
        // 业务逻辑（如清理日志、数据备份等）
        Date now = new Date();
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(now);*/

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();
        SimpleDateFormat yesterdaySdf = new SimpleDateFormat("yyyy-MM-dd");
        String yesterdayStr = yesterdaySdf.format(yesterday);
        System.out.println("昨天的日期: " + yesterdayStr);

        System.out.println("每日任务起止时间: " + yesterdayStr + "T00:00:00.000+08:00" + "-------" + yesterdayStr + "T23:59:50.000+08:00");

        Map<String, String> getDeviceListParamMap = new HashMap<>();
        getDeviceListParamMap.put("flagId", "");
        getDeviceListParamMap.put("offset", "0");
        getDeviceListParamMap.put("pageNo", "1");
        getDeviceListParamMap.put("pageSize", "500");

        JSONObject deviceListJson = JSONObject.parseObject(hikFireCloud.getDeviceList(getDeviceListParamMap));
        JSONObject deviceListDataJson = JSON.parseObject(deviceListJson.get("data").toString());
        List<HikFireCloudDeviceResp.Device> hikFireCloudDeviceResps = JSON.parseArray(deviceListDataJson.get("list").toString(), HikFireCloudDeviceResp.Device.class);
        Map<String, String> getMonitorListParamMap = new HashMap<>();
        for(int i = 0; i < hikFireCloudDeviceResps.size(); i++){
            getMonitorListParamMap.put("deviceID",hikFireCloudDeviceResps.get(i).getDeviceID());
            getMonitorListParamMap.put("flagId",hikFireCloudDeviceResps.get(i).getDeviceID());
            getMonitorListParamMap.put("monitorStartTime", yesterday + "T00:00:00.000+08:00");
            getMonitorListParamMap.put("monitorStopTime", yesterday + "T23:59:50.000+08:00");
            getMonitorListParamMap.put("offset", "0");
            getMonitorListParamMap.put("pageNo", "1");
            getMonitorListParamMap.put("pageSize", "1");
            JSONObject historyMonitorListJson = JSONObject.parseObject(hikFireCloud.getMonitor(getMonitorListParamMap));
            try {
                FixedPointBean fixedPointBean = new FixedPointBean();
                fixedPointBean.setPsFpDeviceId(hikFireCloudDeviceResps.get(i).getDeviceID());
                fixedPointBean.setPsFpHikData(historyMonitorListJson.get("data").toString());
                service.add(fixedPointBean);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
